﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using filmquiz.Src.DatabaseAccess;
using System.Net.Mail;
using MySql.Data.MySqlClient;

namespace filmquiz.Src.Mail
{
    public class MailHelper
    {
        private static MailHelper instance;
        private DbAcces dbAcces;

        private MailHelper()
        {
            dbAcces = DbAcces.getInstance();
        }

        public static MailHelper getInstance()
        {
            if (instance == null)
            {
                instance = new MailHelper();
            }
            return instance;
        }

        public void sendUsernameAndNewPassword(string email, string username, string password)
        {
            string subject = "Brugernavn og password til Filberts Filmquiz";

            string body = "Brugernavn: " + username + "\nPassword: " + password + "\nDu kan ændre dit password når du er logget ind på quizzen.\nVenlig hilsen Filberts Filmquiz";

            string response = WriteEmail(Statics.getInstance().getEmailSender(), email, body, subject);            
        }

        public string WriteEmail(String Sender, String Recipient, String MessageBody, String MessageSubject)
        {
            try
            { //Attempt to create and send an email using the information supplied through the parameters
                SmtpClient myClient = new SmtpClient(Statics.getInstance().getSmtpServer());
                myClient.Send(Sender, Recipient, MessageSubject, MessageBody);
                //If the message was sent successfully return the string "Sent"
                return "sent";
            }
            catch (SmtpException Err)
            {
                //There was an Error so Return the Error in String Format
                return Err.ToString();
            }
        }
    }
}